import { makeObservable, observable, computed, autorun } from "mobx";

class OrderLine {
  price = 0;
  amount = 1;

  constructor(price) {
    makeObservable(this, {
      price: observable,
      amount: observable,
      total: computed,
    });
    this.price = price;
  }

  get total() {
    console.log("Computing...");
    return this.price * this.amount;
  }
}

const order = new OrderLine(0);

const stop = autorun(() => {
  console.log("Total: " + order.total);
});
// Computing...
// Total: 0

console.log(order.total);
// (不会重新计算!)
// 0

order.amount = 5;
// Computing...
// (无需 autorun)

order.price = 2;
// Computing...
// Total: 10

stop();

order.price = 3;
// 计算值和 autorun 都不会被重新计算.
